package org.dromara.basic.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.run.service.IDeptService;
import org.springframework.stereotype.Service;
import org.dromara.basic.domain.bo.TeachClassBo;
import org.dromara.basic.domain.vo.TeachClassVo;
import org.dromara.basic.domain.TeachClass;
import org.dromara.basic.mapper.TeachClassMapper;
import org.dromara.basic.service.ITeachClassService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 教学班级Service业务层处理
 *
 * @author chl
 * @date 2024-09-10
 */
@RequiredArgsConstructor
@Service
public class TeachClassServiceImpl implements ITeachClassService {

    private final TeachClassMapper baseMapper;
    private final IDeptService deptService;

    /**
     * 查询教学班级
     */
    @Override
    public TeachClassVo queryById(Long classId){
        return baseMapper.selectVoById(classId);
    }

    @Override
    public TeachClassVo queryByClassName(String className) {
        TeachClassVo teachClassVo = baseMapper.selectVoOne(new LambdaQueryWrapper<TeachClass>()
            .eq(TeachClass::getClassName, className));
        return teachClassVo;
    }

    /**
     * 查询教学班级列表
     */
    @Override
    public TableDataInfo<TeachClassVo> queryPageList(TeachClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TeachClass> lqw = buildQueryWrapper(bo);
        Page<TeachClassVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询教学班级列表
     */
    @Override
    public List<TeachClassVo> queryList(TeachClassBo bo) {
        LambdaQueryWrapper<TeachClass> lqw = buildQueryWrapper(bo);
        return baseMapper.selectTeachClassList(lqw);
    }

    @Override
    public List<TeachClassVo> queryListByMajor(Integer majorId) {
        return baseMapper.queryListByMajor(majorId);
    }

    private LambdaQueryWrapper<TeachClass> buildQueryWrapper(TeachClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TeachClass> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), TeachClass::getDeptId, bo.getDeptId());
        lqw.eq(bo.getClassId()!=null, TeachClass::getClassId, bo.getClassId());
//        lqw.eq(StringUtils.isNotBlank(bo.getYears()), TeachClass::getYears, bo.getYears());
        lqw.eq(bo.getHeadcount()!=null, TeachClass::getHeadcount, bo.getHeadcount());
        lqw.eq(StringUtils.isNotBlank(bo.getTeacherId()), TeachClass::getTeacherId, bo.getTeacherId());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()), TeachClass::getClassName, bo.getClassName());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            TeachClass::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增教学班级
     */
    @Override
    public Boolean insertByBo(TeachClassBo bo) {
        String s = deptService.selectDeptNameByDeptId(Long.parseLong(bo.getDeptId()));
        if(StringUtils.isNotEmpty(bo.getGradeLabel())&&StringUtils.isNotEmpty(s)){
            bo.setClassName(bo.getGradeLabel()+s);
        }
        TeachClass add = MapstructUtils.convert(bo, TeachClass.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClassId(add.getClassId());
        }
        return flag;
    }
    @Override
    public Boolean insertByB(TeachClassBo bo) {
        TeachClass add = MapstructUtils.convert(bo, TeachClass.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClassId(add.getClassId());
        }
        return flag;
    }

    /**
     * 修改教学班级
     */
    @Override
    public Boolean updateByBo(TeachClassBo bo) {
        String s = deptService.selectDeptNameByDeptId(Long.parseLong(bo.getDeptId()));
        if(StringUtils.isNotEmpty(bo.getGradeLabel())&&StringUtils.isNotEmpty(s)){
            bo.setClassName(bo.getGradeLabel()+s);
        }
        TeachClass update = MapstructUtils.convert(bo, TeachClass.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TeachClass entity){
        QueryWrapper<TeachClass> queryWrapper=new QueryWrapper();
        queryWrapper.eq("class_name",entity.getClassName());
        if(entity.getClassId()!=null){
            queryWrapper.ne("class_id",entity.getClassId());
        }
        List<TeachClassVo> teachClassVos = baseMapper.selectVoList(queryWrapper);
        if (!CollectionUtils.isEmpty(teachClassVos)) {
            throw new ServiceException("班级已经存在");
        }
    }

    /**
     * 批量删除教学班级
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
